﻿//-----------------------------------------------------------------------
// <copyright file="UnitTest.cs" company="Justin Wallace Saunders">
//     Copyright (c) Microsoft Public License. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Community.Tools.Windows.Service.Test
{
    using System;
    using System.IO;
    using System.ServiceProcess;
    using Community.Tools.Windows.Service;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
     /// <summary>
    /// Unit Test for start and stop window service method
    /// </summary>
    [TestClass]
    public class UnitTest
    {
        /// <summary>
        /// Temp file name
        /// </summary>
        private static string tempFile = "FSWTempFile.dll";

        /// <summary>
        /// Temp path
        /// </summary>
        private static string tempPath = @"C:\";

        /// <summary>
        /// Temp File path
        /// </summary>
        private static string tempFilePath = Path.Combine(tempPath, tempFile);
                
        /// <summary>
        /// current test instance
        /// </summary>
        private TestContext testContextInstance;

        /// <summary>
        /// Gets or sets the test context which provides
        /// information about and functionality for the current test run.
        /// </summary>
        public TestContext TestContext
        {
            get
            {
                return this.testContextInstance;
            }

            set
            {
                this.testContextInstance = value;
            }
        }

        #region Additional test attributes
        // You can use the following additional attributes as you write your tests:
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        // Use TestInitialize to run code before running each test
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        #endregion

        /// <summary>
        /// start window service method
        /// </summary>
        [TestMethod]
        public void StartService()
        {
            ServiceController service = new ServiceController("Community.Tools.Windows.Service");
            if (!service.Status.Equals(ServiceControllerStatus.Running))
            {
                service.Start();
                TimeSpan timeout = TimeSpan.FromMilliseconds(5000);
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                Assert.IsTrue(service.Status.Equals(ServiceControllerStatus.Running));
            }
            else
            {
                Assert.IsTrue(true);
            }
        }

        /// <summary>
        /// stop window service method
        /// </summary>
        [TestMethod]
        public void StopService()
        {
            ServiceController service = new ServiceController("Community.Tools.Windows.Service");
            if (service.Status.Equals(ServiceControllerStatus.Running))
            {
                service.Stop();
                TimeSpan timeout = TimeSpan.FromMilliseconds(5000);
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                Assert.IsTrue(service.Status.Equals(ServiceControllerStatus.Stopped));
            }
            else
            {
                Assert.IsTrue(true);
            }
        }

        /// <summary>
        /// A test for Service Constructor
        /// </summary>
        [TestMethod]
        [DeploymentItem("Community.Tools.Windows.Service.exe")]
        public void LoadingPlugin_In_Construtor()
        {
            Settings settings  = new Settings(); // TODO: Initialize to an appropriate value
            Service target = new Service(settings);
            Assert.IsTrue(true);
        }

        /// <summary>
        /// A test for WindowService
        /// </summary>
        [TestMethod]
        [DeploymentItem("Community.Tools.Windows.Service.exe")]
        public void ExcutePlugininThreadTest()
        {
            Settings settings = new Settings();
            PrivateObject param0 = new PrivateObject(new Service(settings)); // TODO: Initialize to an appropriate value
            Service_Accessor target = new Service_Accessor(param0); // TODO: Initialize to an appropriate value
            target.WindowService();
           //// Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        /// A test for FileSystemWatcher_Created
        /// </summary>
        [TestMethod]
        [DeploymentItem("Community.Tools.Windows.Service.exe")]
        public void Loadplugin_Added_in_directoryTest()
        {
            Settings settings = new Settings();
            PrivateObject param0 = new PrivateObject(new Service(settings)); // TODO: Initialize to an appropriate value
            Service_Accessor target = new Service_Accessor(param0); // TODO: Initialize to an appropriate value
            FileSystemWatcher fw = new FileSystemWatcher(settings.PluginDirectory, "*.dll");
            object sender = fw; // TODO: Initialize to an appropriate value
            FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Created, settings.PluginDirectory, tempFile); // TODO: Initialize to an appropriate value
            target.FileSystemWatcher_Created(sender, e);
            ////Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }
    }
}
